﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ServiceWeb.DBGateway;
using UtilsORM;
using GatewayServer.Common;

namespace GatewayServer.Controllers
{
    public class SystemInfoController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        public JsonResult getDeviceInfo()
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();

            keyValuePairs.Add("deviceCount", ef.iot_device.Count().ToString());
            keyValuePairs.Add("deviceErrorCount", ef.iot_device.Where(c => c.d_status != 0).Count().ToString());
            keyValuePairs.Add("deviceOnLineCount", new DBIotDeviceLiveStateCache().getGateWayOnlineCount().ToString());
            //传感器
            keyValuePairs.Add("sensorCount", MySqlYihongIot.GetDataTable("select count(*) from iot_device_model m inner join iot_device on d_model_guid=m.m_guid where m.m_type_guid='984be435-cd43-11ed-8e56-525400caac1e'").Rows[0][0].ToString());
            //阀门
            keyValuePairs.Add("valveCount", MySqlYihongIot.GetDataTable("select count(*) from iot_device_model m inner join iot_device on d_model_guid=m.m_guid where m.m_type_guid in('a1ca02da-cd43-11ed-8e56-525400caac1e','b5e8abce-e4a0-11ed-882c-525400caac1e')").Rows[0][0].ToString());
            //
            keyValuePairs.Add("sensorDataCount", ef.iot_device_value_log.Where(c => c.vl_add_time > DateTime.Now.AddDays(-1)).Count().ToString());
            keyValuePairs.Add("mqDataCount", ef.iot_mq_log.Where(c => c.ml_add_time > DateTime.Now.AddDays(-1)).Count().ToString());
            return ReturnJsonResult.Sucess(keyValuePairs);

        }

    }
}
